#! /usr/bin/bash

DestDir="/home/lhy/smarthome/edge_rk3308/software/build/edge_midware"

#拉取文件
function pullFile(){
	local file=$1
	local panelSelection="-s ${panelAddress}"
	if [ ${panelAddress} == "local" ]; then
		panelSelection=""
	fi
	adb ${panelSelection} pull /data/changhong/edge_midware/${file} ${DestDir}
}

#显示并构造支持拉取的文件
function echoAndBuildPullArray(){
cat << _EOF_
-----------------------------
site:
	[1]:	zigbee_light_service_device_list.json
	[2]: 	zigbee_light_log.txt
	[3]: 	
	[4]: 	
	[5]: 	
-----------------------------
_EOF_

	pullArray[1]=service_site_config/zigbee_light_service_device_list.json
	pullArray[2]=service_site_config/zigbee_light_log.txt
	pullArray[3]=
	pullArray[4]=
	pullArray[5]=
}

# 列出当前已连接设备列表
adb devices

#选择操作终端，如果没有则建立新链接
read -p "panel address: " panelAddress
if [[ $panelAddress =~ ^[0-9]+.[0-9]+.[0-9]+.[0-9]+ ]]; then
	echo "panelAddress: ${panelAddress}"	
	adb connect ${panelAddress}
	while [ 1 ]
	do
		$(adb -s ${panelAddress} shell byjs2023) 
		result=$?
		if [[ $result != 0 ]]; then
			sleep 2
			continue
		else
			break
		fi
	done
elif [ $panelAddress == "local" ]; then
	echo "local host ..."
else
	echo "please input valid panel ip address or local"
	exit
fi

echoAndBuildPullArray


read -p "choose files to pull: " -a array
for index in ${array[@]}
do
	echo  ${pullArray[${index}]}
	pullFile ${pullArray[${index}]} 
done

